/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.ka.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaaction.common.persistence.Page;
import com.kaaction.common.service.CrudService;
import com.kaaction.modules.ka.entity.KaFinance;
import com.kaaction.modules.ka.dao.KaFinanceDao;

/**
 * 财务报表Service
 * @author ZengBiao
 * @version 2017-06-23
 */
@Service
@Lazy(false)
@Transactional(readOnly = true)
public class KaFinanceService extends CrudService<KaFinanceDao, KaFinance> {

	public KaFinance get(String id) {
		return super.get(id);
	}
	
	public List<KaFinance> findList(KaFinance kaFinance) {
		return super.findList(kaFinance);
	}
	
	public Page<KaFinance> findPage(Page<KaFinance> page, KaFinance kaFinance) {
		return super.findPage(page, kaFinance);
	}
	
	@Transactional(readOnly = false)
	public void save(KaFinance kaFinance) {
		super.save(kaFinance);
	}
	
	@Transactional(readOnly = false)
	public void delete(KaFinance kaFinance) {
		super.delete(kaFinance);
	}
	
	//@Scheduled(cron = "1 0 0 1 * ?")	 //线上，每月1日，00:00:01执行任务
	//@Scheduled(cron = "10 * * * * ?")  //测试用途
	@Transactional(readOnly = false)
    public void summarize(Date beginDate){
		if(beginDate != null){
			int year = beginDate.getYear() + 1900;
			int month = beginDate.getMonth();
			String monthStr = String.valueOf(month);
			if(month < 10)monthStr = "0" + monthStr;
			
			int nextMonth = beginDate.getMonth() + 1;
			String nextMonthStr = String.valueOf(nextMonth);
			if(nextMonth < 10)nextMonthStr = "0" + nextMonthStr;
			
			String start = year + "-" + monthStr + "-01 00:00:00";
			String end = year + "-" + nextMonthStr + "-01 00:00:00";
	        
	        super.dao.batchDel(year, month);
			super.dao.batchInit(start,end,year,month);
		}
    }
	
	public static void main(String[] args){

	}
	
	private Date getLastMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);
        return cal.getTime();
    }
	
	private String end(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    return sdf.format(date);
	}

	public List<KaFinance> findAllListByMonth(String year, String month) {
		return super.dao.findAllListByMonth(year,month);
	}
	
	
}